﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    public class SysFileService
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SysFile model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SysFile(");
            strSql.Append("Code,RealName,FileName,FilePath,ThumbnailsName,ThumbnailsPath,PreviewName,PreviewPath,TourismCode,Remark,State,CreateID,CreateTime,Sort,IsCover)");
            strSql.Append(" values (");
            strSql.Append("@Code,@RealName,@FileName,@FilePath,@ThumbnailsName,@ThumbnailsPath,@PreviewName,@PreviewPath,@TourismCode,@Remark,@State,@CreateID,@CreateTime,@Sort,@IsCover)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
					new SqlParameter("@RealName", SqlDbType.NVarChar,200),
					new SqlParameter("@FileName", SqlDbType.NVarChar,200),
                    new SqlParameter("@FilePath", SqlDbType.NVarChar,500),
                    new SqlParameter("@ThumbnailsName", SqlDbType.NVarChar,200),
					new SqlParameter("@ThumbnailsPath", SqlDbType.NVarChar,500),
                    new SqlParameter("@PreviewName", SqlDbType.NVarChar,200),
					new SqlParameter("@PreviewPath", SqlDbType.NVarChar,500),
					new SqlParameter("@TourismCode", SqlDbType.VarChar,36),
                   new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@IsCover", SqlDbType.Bit)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.RealName;
            parameters[2].Value = model.FileName;
            parameters[3].Value = model.FilePath;
            parameters[4].Value = model.ThumbnailsName;
            parameters[5].Value = model.ThumbnailsPath;
            parameters[6].Value = model.PreviewName;
            parameters[7].Value = model.PreviewPath;
            parameters[8].Value = model.TourismCode;
            parameters[9].Value = model.Remark;
            parameters[10].Value = model.State;
            parameters[11].Value = model.CreateID;
            parameters[12].Value = model.CreateTime;
            parameters[13].Value = model.Sort;
            parameters[14].Value = model.IsCover;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SysFile model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SysFile set ");
            strSql.Append("RealName=@RealName,");
            strSql.Append("FileName=@FileName,");
            strSql.Append("FilePath=@FilePath,");
            strSql.Append("ThumbnailsName=@ThumbnailsName,");
            strSql.Append("ThumbnailsPath=@ThumbnailsPath,");
            strSql.Append("PreviewName=@PreviewName,");
            strSql.Append("PreviewPath=@PreviewPath,");
            strSql.Append("TourismCode=@TourismCode,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("IsCover=@IsCover");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {                                          
						new SqlParameter("@RealName", SqlDbType.NVarChar,200),
					new SqlParameter("@FileName", SqlDbType.NVarChar,200),
                    new SqlParameter("@FilePath", SqlDbType.NVarChar,500),
                    new SqlParameter("@ThumbnailsName", SqlDbType.NVarChar,200),
					new SqlParameter("@ThumbnailsPath", SqlDbType.NVarChar,500),
                    new SqlParameter("@PreviewName", SqlDbType.NVarChar,200),
					new SqlParameter("@PreviewPath", SqlDbType.NVarChar,500),
					new SqlParameter("@TourismCode", SqlDbType.VarChar,36),
                   new SqlParameter("@Remark", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@IsCover", SqlDbType.Bit),
					new SqlParameter("@Code", SqlDbType.VarChar,36)};
            parameters[0].Value = model.RealName;
            parameters[1].Value = model.FileName;
            parameters[2].Value = model.FilePath;
            parameters[3].Value = model.ThumbnailsName;
            parameters[4].Value = model.ThumbnailsPath;
            parameters[5].Value = model.PreviewName;
            parameters[6].Value = model.PreviewPath;
            parameters[7].Value = model.TourismCode;
            parameters[8].Value = model.Remark;
            parameters[9].Value = model.State;
            parameters[10].Value = model.CreateID;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.Sort;
            parameters[13].Value = model.IsCover;
            parameters[14].Value = model.Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SysFile ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SysFile GetModel(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Code,RealName,FileName,FilePath,ThumbnailsName,ThumbnailsPath,PreviewName,PreviewPath,TourismCode,Remark,State,CreateID,CreateTime,Sort,IsCover from SysFile ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            SysFile model = new SysFile();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<SysFile> GetAll(int pageIndex, int pageSize, out int RecordCount)
        {
            string strsql = @"select Code,RealName,FileName,FilePath,ThumbnailsName,ThumbnailsPath,PreviewName,PreviewPath,TourismCode,Remark,State,CreateID,CreateTime,Sort,IsCover from SysFile order by sort desc";
            List<SysFile> list = new List<SysFile>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<SysFile> GetAll()
        {
            string strsql = @"select  Code,RealName,FileName,FilePath,ThumbnailsName,ThumbnailsPath,PreviewName,PreviewPath,TourismCode,Remark,State,CreateID,CreateTime,Sort,IsCover from SysFile  order by sort desc";
            List<SysFile> list = new List<SysFile>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SysFile DataRowToModel(SqlDataReader row)
        {
            SysFile model = new SysFile();
            if (row != null)
            {
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["RealName"] != null)
                {
                    model.RealName = row["RealName"].ToString();
                }
                if (row["FileName"] != null)
                {
                    model.FileName = row["FileName"].ToString();
                }
                //  Code,RealName,FileName,FilePath,ThumbnailsName,ThumbnailsPath,,,,Remark,
                if (row["FilePath"] != null)
                {
                    model.FilePath = row["FilePath"].ToString();
                }
                if (row["ThumbnailsName"] != null)
                {
                    model.ThumbnailsName = row["ThumbnailsName"].ToString();
                }
                if (row["ThumbnailsPath"] != null)
                {
                    model.ThumbnailsPath = row["ThumbnailsPath"].ToString();
                }
                if (row["PreviewName"] != null)
                {
                    model.PreviewName = row["PreviewName"].ToString();
                }
                if (row["PreviewPath"] != null)
                {
                    model.PreviewPath = row["PreviewPath"].ToString();
                }
                if (row["TourismCode"] != null)
                {
                    model.TourismCode = row["TourismCode"].ToString();
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["CreateID"] != null)
                {
                    model.CreateID = row["CreateID"].ToString();
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["IsCover"] != DBNull.Value)
                {
                    model.IsCover = bool.Parse(row["IsCover"].ToString());
                }
            }
            return model;
        }
    }
}
